import tui from '../../common/httpRequest'
const app = getApp()
import QQMapWX from '../../utils/qqmap-wx-jssdk.min';
let qqmapsdk
Page({
  data: {
    jigou:[],
    anli:[],
    login:false,
    data:{},
    city:[],
    selcity:{},
    tuijian:[]
  },

  topage(e){
    console.log(e)
    wx.navigateTo({
      url: e.currentTarget.dataset.path,
    })
  },

  emit(n,c){
    console.log(n)
    console.log(c)

    let sc = {
      name:n,
      code:c
    }

    wx.setStorageSync('selCity', sc)
    this.setData({
      selcity:sc
    })
  },
  
  onLoad(getopenid) {

    qqmapsdk = new QQMapWX({
      key: 'UXJBZ-LPCWG-2NKQ3-IRMKM-GVNJZ-G7B6X'    // 必填
   });


    let that = this
    tui.login(function(e){
      that.setData({
        login:true
      })
      wx.setStorageSync('token', e)
      that.getData()
    })
    this.getLocation()
    this.getJigou()
    this.getanli()
  },

  getTuijian(){
    let ct = wx.getStorageSync('selCity')
    let cc = '86'
    if(ct!=null){
      cc = ct.code
    }
    tui.request('/baoxian/api/getTuijian','GET',{cityCode:cc},false,true,true).then((res)=>{
      this.setData({
        tuijian:res.data
      })
    })
  },

  checkAuth(callback) {
    let that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.getLocation({
                type: 'wgs84',
                success(re) {
                  callback(re.latitude, re.longitude)
                }
              })
            },
            fail(){
              that.getTuijian()
            },
          })
        }else{
          wx.getLocation({
            type: 'wgs84',
            success(re) {
              callback(re.latitude, re.longitude)
            }
          })
        }
      }
    })
  },

  getLocation(){
    let that = this
    this.checkAuth((lat,log) => {
      console.log(lat)
      qqmapsdk.reverseGeocoder({
        sig: 'S0hEBftlajqgU46fslsY6AWfyVYW1YPc',    // 必填
         location: {latitude:lat, longitude:log},
         success(res) {
            let code = res.result.ad_info.phone_area_code
            wx.setStorageSync('cityCode', code);
            that.getCityByCode(code)
             console.log(res)
         },
         fail(err) {
            console.log(err)
         },
         complete() {
            // 做点什么

         }
      })
    })
  },

  getCityByCode(e){
    tui.request('/baoxian/api/getCityByCode','GET',{cityCode:e},false,true,true).then((res)=>{
      let da = {
        name:res.data.shortName,
        code:res.data.cityCode
      }
      wx.setStorageSync('selCity', da)
      this.setData({
        selcity:da
      },()=>{
        this.getTuijian()
      })
    })
  },

  getData(){
    tui.request('/baoxian/api/getMineData','GET',{},false,true,true).then((res)=>{
      this.setData({
        data:res.data
      })
    })
  },

  getJigou(){
    tui.request('/baoxian/api/companyList','GET',{},false,true,true).then((res)=>{
      this.setData({
        jigou:res.data
      })
    })
  },

  getanli(){
    tui.request('/baoxian/api/getAnli','GET',{pageNum:1,pageSize:3},false,true,true).then((res)=>{
      this.setData({
        anli:res.data
      })
    })
  },

  checkToken(){
    tui.request('/system/user/checkToken','GET',{},false,true,true).then((res)=>{
      if(res.code==200){
        if(res.user == null){
          this.setData({
            login:false
          })
        }else{
          this.setData({
            login:true
          })
          this.getData()
        }
        // tui.login()
      }
    })
  },

  onShow(){
    this.checkToken()
    this.getTuijian()
  },
  onShareTimeline(){
    return {
      title:'学平宝，学平险自助投保平台'
    }
  },
  onShareAppMessage(){}
})
